
#include <stdint.h>
#include <stdbool.h> 


//#include "stm32f1xx_hal.h"


#include "spi.h"
#include "sx1278-Hal.h"

//#if defined( USE_SX1276_RADIO )

void SX1276InitIo( void )
{
    // Configure NSS as output
    HAL_GPIO_WritePin(NSS_GPIO_Port, NSS_Pin, GPIO_PIN_SET);
}


void SX1276SetReset( uint8_t state )
{
    GPIO_InitTypeDef GPIO_InitStruct;

    if( state == RADIO_RESET_ON )
    {
        // Set RESET pin to 0
        HAL_GPIO_WritePin(GPIOA, REST_Pin, GPIO_PIN_RESET);

        // Configure RESET as output

        GPIO_InitStruct.Pin = REST_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
    else
    {
        HAL_GPIO_WritePin( GPIOA, REST_Pin, GPIO_PIN_SET );
    }
}

void SX1276Write( uint8_t addr, uint8_t data )
{
    SX1276WriteBuffer( addr, &data, 1 );
}

void SX1276Read( uint8_t addr, uint8_t *data )
{
    SX1276ReadBuffer( addr, data, 1 );
}

void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;

    //NSS = 0;
    HAL_GPIO_WritePin( NSS_GPIO_Port, NSS_Pin, GPIO_PIN_RESET );

    HW_SPI_InOut( addr | 0x80 );
    for( i = 0; i < size; i++ )
    {
        HW_SPI_InOut( buffer[i] );
    }

    //NSS = 1;
    HAL_GPIO_WritePin( NSS_GPIO_Port, NSS_Pin, GPIO_PIN_SET );
}

void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;

    //NSS = 0;
    HAL_GPIO_WritePin( NSS_GPIO_Port, NSS_Pin, GPIO_PIN_RESET );

    HW_SPI_InOut( addr & 0x7F );

    for( i = 0; i < size; i++ )
    {
        buffer[i] = HW_SPI_InOut(0);
    }

    //NSS = 1;
    HAL_GPIO_WritePin( NSS_GPIO_Port, NSS_Pin, GPIO_PIN_SET );
}

void SX1276WriteFifo( uint8_t *buffer, uint8_t size )
{
    SX1276WriteBuffer( 0, buffer, size );
}

void SX1276ReadFifo( uint8_t *buffer, uint8_t size )
{
    SX1276ReadBuffer( 0, buffer, size );
}

inline uint8_t SX1276ReadDio0( void )
{
    return HAL_GPIO_ReadPin( DIO0_GPIO_Port, DIO0_Pin );
}


inline void SX1276WriteRxTx( uint8_t txEnable )
{
   
}

//#endif // USE_SX1276_RADIO

